<?php
namespace Admin\Model;
use Common\Model\FileModel as BashModel;
use Common\Model\LogInfoModel;
use Common\Model\FileLinkModel;
use Think\Image;
/**
 * 文件相关表操作
 * 
 * @author xiaoyutab<xiaoyutab@qq.com>
 * @version v1.0.0
 * @copyright (c) 2017, xiaoyutab
 * @adtime 2017-07-13 20:32:43
 */
class FileModel extends BashModel {
    protected $link = null;
    /**
     * 获取表关联的全部图片信息
     * @param type $table 表名
     * @param type $id 表主键编号
     * @return array 查询到的列表
     */
    public function get_img_all($table = '',$id = 0,$onece = false,$file_name = false){
        if(!is_numeric($table)){
            // 如果table表不是数字
            $table = (new LogInfoModel())->get_id($table);
        }
        if(!$table){
            return array();
        }
        // 获取文件图片，最多99条
        $where = array('target_id'=>$id,'target_name'=>$table);
        if($file_name !== FALSE){
            if($file_name){
                $where['file_name'] = trim($file_name);
            } else {
                $where['file_name'] = array('exp','IS NULL');
            }
        }
        $list = $this->get_link()->where($where)->getList(0,99);
        foreach($list as $k => $v){
            $list[$k]['file_content'] = $this->get_info($v['file_id']);
        }
        if($onece){
            return $list[0];
        }
        return $list;
    }
    
    /**
     * 获取日志详情表
     * @return type
     */
    public function get_link(){
        if($this->link){
            return $this->link;
        }
        $this->link = new FileLinkModel();
        return $this->link;
    }
    
    /**
     * 回滚之前标记的删除标记
     * @param type $lid 目标表编号
     * @param type $table 目标表名
     * @return boolean
     */
    public function call_back($lid = 0,$table = 'project'){
        if($lid <= 0){
            return FALSE;
        }
        $log = new LogInfoModel();
        $table = $log->get_id($table);
        return $this->get_link()->where(array(
            'target_id' => $lid,
            'target_name' => $table,
            'status' => 97
        ))->save(array(
            'status' => 99,
            'del_time' => 0,
        ));
    }
    
    /**
     * 回滚之前标记的删除标记
     * @param type $lid 目标表编号
     * @param type $table 目标表名
     * @return boolean
     */
    public function submit_back($lid = 0,$table = 'project'){
        if($lid <= 0){
            return FALSE;
        }
        $log = new LogInfoModel();
        $table = $log->get_id($table);
        return $this->get_link()->where(array(
            'target_id' => $lid,
            'target_name' => $table,
            'status' => 97,
        ))->save(array(
            'status' => 98
        ));
    }
    
    /**
     * 获取表编号
     * 
     * @param type $tab
     * @return type
     */
    public function get_table_id($tab = ''){
        $log = new LogInfoModel();
        return $log->get_id($tab);
    }

    /**
     * 检测图片上传结果
     * 
     * @param type $post
     * @return type
     */
    public function check_info($post = array()) {
        $id = $this->where(array(
            'ext' => $post['ext'],
            'key' => $post['type'],
            'md5' => $post['md5'],
            'sha1' => $post['sha1'],
            'size' => $post['size'],
        ))->cache(TRUE)->find();
        if($id){
            // 如果图片存在则直接删除
            (new \Org\Util\SFile())->rm_dirs(C('Upload.IMAGE_SAVE_PATH').$post['savepath'].$post['savename']);
            return $id['id'];
        }
        $post['key'] = $post['type'];
        $post['file_path'] = $post['savepath'].$post['savename'];
        $thumb = $this->thumb($post['file_path']);
        $post['thumb_100'] = $thumb['100x100']['status']?$thumb['100x100']['file_path']:'';
        $post['thumb_200'] = $thumb['200x200']['status']?$thumb['200x200']['file_path']:'';
        return $this->create_info($post);
    }

    /**
     * 生成缩略图
     *       备注：生成缩略图所在目录为$file文件所在目录
     * @param string $file 文件所在位置，配置项IMAGE_SAVE_PATH所配置文件夹后的文件
     * @param array $date 缩略图大小数组。格式为[[100,100],[50,50]]
     * @param booler $thumbRemoveOrigin 是否删除原图，默认不删除false
     * @return array 缩略图文件数组，格式为[['file_path'=>'xxxxx','status'=>true],[...]]
     */
    private function thumb($file = '', $date = array(array(100, 100), array(200, 200)), $thumbRemoveOrigin = false) {
        if (empty($file)) {
            return false;
        }
        $image = new Image();
        $returns = array();
        foreach ($date as $k => $v) {
            $file_path =  dirname($file) . '/thumb_' . $v[0] . 'x' . $v[1] . '_' . basename($file);
            //打开要处理的图片
            $image->open(C('Upload.IMAGE_SAVE_PATH', null, './Public/Upload/') . $file);
            $image->thumb($v[0], $v[1], Image::IMAGE_THUMB_FIXED)->save(C('Upload.IMAGE_SAVE_PATH', null, './Public/Upload/').$file_path);
            $returns[$v[0].'x'.$v[1]] = array(
                'file_path' => $file_path,
                'status' => is_file(C('Upload.IMAGE_SAVE_PATH', null, './Public/Upload/').$file_path)
            );
        }
        if ($thumbRemoveOrigin) {
            unlink(C('IMAGE_SAVE_PATH', null, './Public/Upload/') . $file);
        }
        return $returns;
    }
}